<?php

/**
 * 法拍百科模型
 */

namespace app\api\model;


use app\common\lib\RedisKey;
use app\common\lib\RespCode;
use app\common\model\BaseModel;
use custom\Token;
use PDOStatement;
use think\Collection;
use think\Db;
use think\db\exception\DataNotFoundException;
use think\db\exception\ModelNotFoundException;
use think\Exception;
use think\exception\DbException;
use think\exception\PDOException;

class wiki extends BaseModel {

    const TYPE = [0, 1, 2, 3];

    //点赞数
    protected static $giveNum = 1;

    /**
     * 法拍百科列表数据
     * @param $params
     * @return array|PDOStatement|string|Collection
     * @throws Exception
     * @throws DataNotFoundException
     * @throws ModelNotFoundException
     * @throws DbException
     */
    public static function getList($params, $headers) {
        if (empty($params['type'])) {
            $where = 1;
        } else {
            if (!in_array($params['type'], self::TYPE)) {
                throw new Exception('错误的类别', RespCode::PARAMS_ERR);
            } else {
                $where[] = ['type', '=', $params['type']];
            }
        }
        empty($params['start']) ? $start = 1 : $start = intval($params['start']);
        empty($params['long']) ? $long = 10 : $long = intval($params['long']);
        $data = db('wiki')
            ->field('cover,id,title,release_time,play_num,init_num,give_num,init_give_num,detail_url,filename')
            ->where($where)
            ->order('itime', 'desc')
            ->page($start, $long)
            ->select();
        foreach ($data as $k => $v) {
            $data[$k]['detail_url'] = empty($data[$k]['filename']) ? '' : config('global.wiki_link') . base64_encode($data[$k]['id']);
            $data[$k]['release_time'] = date('Y-m-m H:i:s', $v['release_time']);
            $data[$k]['play_num'] = $v['play_num'] + $v['init_num'];
            $data[$k]['give_num'] = $v['give_num'] + $v['init_give_num'];
            unset($data[$k]['init_num']);
            unset($data[$k]['init_give_num']);
            unset($data[$k]['filename']);
            if (isset($headers['token'])) {
                $Token = new Token();
                $tokens = $Token->verifyToken($headers['token'], $headers['devsn'], $Token::API_TOKEN, RedisKey::APP_TOKEN_HASH);
                if (empty($tokens)) {
                    $data[$k]['is_zan'] = 0;
                } else {
                    $params['agent_id'] = $tokens['id'];
                    if (WikiGive::is_has($params['agent_id'], $data[$k]['id'])) {
                        $data[$k]['is_zan'] = 1;
                    } else {
                        $data[$k]['is_zan'] = 0;
                    }
                }
            } else {
                $data[$k]['is_zan'] = 0;
            }
        }
        $count = db('wiki')
            ->field('cover,id,title,release_time,play_num,init_num,give_num,init_give_num,detail_url')
            ->where($where)
            ->count();
        $page = $start;
        if ($long > 0) {
            $pageNum = ceil($count / $long);
        } else {
            $pageNum = 1;
        }
        return [$data, $count, $page, $pageNum];
    }

    /**
     * 添加点赞，阅读+1
     * @param $params
     * @throws Exception
     * @throws PDOException
     */
    public static function add($params) {
        db('wiki')->where('id', $params['wiki_id'])->setInc('give_num', self::$giveNum);
        db('wiki')->where('id', $params['wiki_id'])->setInc('play_num', 1);
    }

    /**
     * 删除点赞
     * @param $params
     * @return int|string
     * @throws Exception
     * @throws PDOException
     */
    public static function del($params) {
        db('wiki')->where('id', $params['wiki_id'])->setDec('give_num', self::$giveNum);
    }

    /**
     * 阅读
     * @param $params
     * @throws Exception
     * @throws PDOException
     */
    public static function read($params) {
        $oldplay_num = Db::name('wiki')->getFieldByid($params['wiki_id'], 'play_num');
        db('wiki')->where('id', $params['wiki_id'])->update(['play_num' => intval($oldplay_num) + 1]);
    }

}
